Leadtools.Barcode Namespace > BarcodeReader Class > ReadBarcodes Method : ReadBarcodes(RasterImage,LeadRect,Int32,BarcodeSymbology[],IBarcodeReadOptions[]) Method |
For information about this method please see ReadBarcodes(RasterImage,LogicalRectangle,Int32,BarcodeSymbology[],BarcodeReadOptions[]).
public BarcodeData[] ReadBarcodes( RasterImage image, LeadRect searchBounds, int maximumBarcodes, BarcodeSymbology[] symbologies, IBarcodeReadOptions[] options )
'Declaration Public Overloads Function ReadBarcodes( _ ByVal image As RasterImage, _ ByVal searchBounds As LeadRect, _ ByVal maximumBarcodes As Integer, _ ByVal symbologies() As BarcodeSymbology, _ ByVal options() As IBarcodeReadOptions _ ) As BarcodeData()
'Usage Dim instance As BarcodeReader Dim image As RasterImage Dim searchBounds As LeadRect Dim maximumBarcodes As Integer Dim symbologies() As BarcodeSymbology Dim options() As IBarcodeReadOptions Dim value() As BarcodeData value = instance.ReadBarcodes(image, searchBounds, maximumBarcodes, symbologies, options)
public BarcodeData[] ReadBarcodes( RasterImage image, LeadRect searchBounds, int maximumBarcodes, BarcodeSymbology[] symbologies, IBarcodeReadOptions[] options )
ObjectiveC Syntax
function Leadtools.Barcode.BarcodeReader.ReadBarcodes(RasterImage,LeadRect,Int32,BarcodeSymbology[],IBarcodeReadOptions[])( image , searchBounds , maximumBarcodes , symbologies , options )
public: array<BarcodeData^>^ ReadBarcodes( RasterImage^ image, LeadRect searchBounds, int maximumBarcodes, array<BarcodeSymbology>^ symbologies, array<IBarcodeReadOptions^>^ options )
Note: In LEADTOOLS for .NET, the equivalent to Leadtools.LeadRect is Leadtools.Forms.LogicalRectangle, also the equivalent to IBarcodeReadOptions is BarcodeReadOptions.
// This class holds the data we will pass to each thread public class MyThreadData { // This worker thread name public string Name; // Barcode reader instance public BarcodeReader BarcodeReaderInstance; // Array of options to use public IBarcodeReadOptions[] ReadOptions; // Image file containing the barcodes public string ImageFileName; // This will hold the barcodes found public BarcodeData[] Barcodes; // In case of errors public Exception Error; // RasterImage for ImageFileName public RasterImage Image; } [TestMethod] public async Task BarcodeReader_ReadBarcodeExample6() { string originalImageFileName = @"Assets\Barcode1.tif"; string rotatedImageFileName = @"Barcode1_Rotated90.tif"; string[] imageFileNames = { originalImageFileName, rotatedImageFileName }; // Create a 90 degrees rotated version of Barcode1.tif StorageFile loadFile = null; using (RasterCodecs codecs = new RasterCodecs()) { loadFile = await Tools.AppInstallFolder.GetFileAsync(originalImageFileName); using (RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile))) { RotateCommand rotate = new RotateCommand(90 * 100, RotateCommandFlags.Resize, RasterColorHelper.FromKnownColor(RasterKnownColor.White)); rotate.Run(image); StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(rotatedImageFileName); await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.CcittGroup4, 1); } } // Create a Barcode engine BarcodeEngine engine = new BarcodeEngine(); // Get the Barcode reader instance BarcodeReader reader = engine.Reader; // Get a version of all read options that support rotated barcodes IBarcodeReadOptions[] verticalBarcodeReadOptions = GetVerticalReadBarcodeOptions(reader); // Tasks to wait for Task[] finishedTasks = new Task[2]; // Create the thread data objects MyThreadData[] threadsData = new MyThreadData[2]; for(int i = 0; i < threadsData.Length; i++) { threadsData[i] = new MyThreadData(); threadsData[i].BarcodeReaderInstance = reader; threadsData[i].ReadOptions = null; } // Setup the read options for the two threads using (RasterCodecs codecs = new RasterCodecs()) { threadsData[0].Name = "Read Default Options Thread"; threadsData[0].ReadOptions = null; // Default, or horizontal loadFile = await Tools.AppInstallFolder.GetFileAsync(originalImageFileName); threadsData[0].Image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile)); threadsData[1].Name = "Read Vertical Barcodes Thread"; threadsData[1].ReadOptions = verticalBarcodeReadOptions; loadFile = await Tools.AppLocalFolder.GetFileAsync(rotatedImageFileName); threadsData[1].Image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile)); } List<BarcodeData> totalBarcodes = new List<BarcodeData>(); // Now loop through all the images and try to read the barcodes with both threads at the same time foreach(string imageFileName in imageFileNames) { Debug.WriteLine("Reading barcodes from {0}", imageFileName); for(int i = 0; i < 2; i++) { // Clear the previous results (if any), set up the file name and run threadsData[i].Barcodes = null; threadsData[i].Error = null; threadsData[i].ImageFileName = imageFileName; finishedTasks[i] = Task.Factory.StartNew(ReadBarcodesInThread, threadsData[i]); } // Wait till all the tasks finishes Task.WaitAll(finishedTasks); // Add the barcodes found to our list for (int i = 0; i < 2; i++) { if (threadsData[i].Error != null) { Debug.WriteLine("{0} had error {1}", threadsData[i].Name, threadsData[i].Error); } else if (threadsData[i].Barcodes != null) { totalBarcodes.AddRange(threadsData[i].Barcodes); } } } // We are done, show the total number of barcodes read Debug.WriteLine("Done. Total barcodes read: {0}", totalBarcodes.Count); } private void ReadBarcodesInThread(object state) { MyThreadData threadData = state as MyThreadData; Debug.WriteLine("{0} is reading the barcodes", threadData.Name); try { // Read the barcodes using our options BarcodeSymbology[] symbologies = { BarcodeSymbology.Unknown }; threadData.Barcodes = threadData.BarcodeReaderInstance.ReadBarcodes(threadData.Image, LeadRectHelper.Empty, 0, symbologies, null); Debug.WriteLine("{0} has read {1} barcodes", threadData.Name, threadData.Barcodes.Length); } catch (Exception ex) { // Return the error to main threadData.Error = ex; } finally { } } private static IBarcodeReadOptions[] GetVerticalReadBarcodeOptions(BarcodeReader reader) { // By default, the options read horizontal barcodes only, create an array of options capable of reading vertical barcodes // Notice, we cloned the default options in reader so we will not change the original options OneDBarcodeReadOptions oneDReadOptions = reader.GetDefaultOptions(BarcodeSymbology.UPCA).Clone() as OneDBarcodeReadOptions; oneDReadOptions.SearchDirection = BarcodeSearchDirection.Vertical; FourStateBarcodeReadOptions fourStateReadOptions = reader.GetDefaultOptions(BarcodeSymbology.USPS4State).Clone() as FourStateBarcodeReadOptions; fourStateReadOptions.SearchDirection = BarcodeSearchDirection.Vertical; PostNetPlanetBarcodeReadOptions postNetPlanetReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PostNet).Clone() as PostNetPlanetBarcodeReadOptions; postNetPlanetReadOptions.SearchDirection = BarcodeSearchDirection.Vertical; GS1DatabarStackedBarcodeReadOptions gs1StackedReadOptions = reader.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked).Clone() as GS1DatabarStackedBarcodeReadOptions; gs1StackedReadOptions.SearchDirection = BarcodeSearchDirection.Vertical; PatchCodeBarcodeReadOptions patchCodeReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PatchCode).Clone() as PatchCodeBarcodeReadOptions; patchCodeReadOptions.SearchDirection = BarcodeSearchDirection.Vertical; PDF417BarcodeReadOptions pdf417ReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PDF417).Clone() as PDF417BarcodeReadOptions; pdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical; MicroPDF417BarcodeReadOptions microPdf417ReadOptions = reader.GetDefaultOptions(BarcodeSymbology.MicroPDF417).Clone() as MicroPDF417BarcodeReadOptions; microPdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical; // Even though this array will not contain all options, it should be enough to read all barcodes, since the version of ReadBarcodes we will use // will use the default options if an overriden is not passed IBarcodeReadOptions[] readOptions = { oneDReadOptions, fourStateReadOptions, postNetPlanetReadOptions, gs1StackedReadOptions, patchCodeReadOptions, pdf417ReadOptions, microPdf417ReadOptions }; return readOptions; }
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2